package com.white.hot.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.white.hot.dto.User;
import com.white.hot.dto.resp.BaseResp;
import com.white.hot.dto.resp.MapInfo;
import com.white.hot.service.UserService;
import com.white.hot.utils.OkHttpManager;
import com.white.hot.utils.TextUtils;
import com.white.hot.utils.OkHttpManager.ResultCallback;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController
{
	
	private final String appKey = "1bd45d0135814";
	private final String url = "https://webapi.sms.mob.com/sms/verify";
	private OkHttpManager manager;

	@Resource
	private UserService userService;
	
	@RequestMapping("/testmap")
	@ResponseBody
	private Object map(@RequestParam("phone") String phone,HttpServletRequest request, HttpServletResponse resp)
	{
		BaseResp br = new BaseResp();		
		return buildSuccessResp(br);
	}
	
	@RequestMapping("/getRegisterCode")
	private @ResponseBody Object varify(@RequestParam("phone") String phone,
			HttpServletRequest request, final HttpServletResponse response)
	{
		BaseResp br = buildSuccessResp(new BaseResp());
		return br;
//		manager = OkHttpManager.getInstance();
//		if(TextUtils.isEmpty(phone) || TextUtils.isEmpty(zone) || TextUtils.isEmpty(code))
//		{
//			BaseResp br = buildParamIncompleteResp(new BaseResp());
//			Gson gson = new Gson();
//			String content = gson.toJson(br);
//			writeResponse(response, content);
//		}else
//		{
//			Map<String ,String> param = new HashMap<String,String>();
//			param.put("appkey", appKey);
//			param.put("phone", phone);
//			manager.doPost(url, new ResultCallback<String>()
//			{
//
//				@Override
//				public void onError(String simpleMsg, Exception e)
//				{
//					BaseResp br = buildInnerErrorResp(new BaseResp());
//					br.setInfo(simpleMsg);
//					Gson gson = new Gson();
//					String content = gson.toJson(br);
//					writeResponse(response, content);
//				}
//
//				@Override
//				public void onSuccess(String result)
//				{
//					BaseResp br = buildSuccessResp(new BaseResp());
//					br.setInfo(result);
//					Gson gson = new Gson();
//					String content = gson.toJson(br);
//					writeResponse(response, content);
//				}
//			}, param);
//		}
	}
	
	private void writeResponse(HttpServletResponse response,String content)
	{
		PrintWriter  w = null;
		try
		{
			response.setCharacterEncoding("UTF-8");
			w = response.getWriter();
			w.write(content);
			w.flush();
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			if(w != null)
			{
				w.close();
			}
		}
	}

	@RequestMapping("/dologin")
	@ResponseBody
	private Object registByPhone(@RequestParam("suTel") String phone,@RequestParam("suPassword") String password,
			HttpServletRequest request, HttpServletResponse resp)
	{
		BaseResp br = new BaseResp();
		if(TextUtils.isEmpty(phone) || TextUtils.isEmpty(password))
		{
			br = buildParamIncompleteResp(new BaseResp());
			return br;
		}else
		{
			User u = userService.login(phone, password);
			if(u != null)
			{
				buildSuccessResp(br);
				MapInfo mi = new MapInfo();
				mi.user = u;
				br.setMapInfo(mi);
				return br;
			}else
			{
				br.setInfo("用户名或者密码错误");
				return buildInnerErrorResp(br);
			}
		}
	}
	
	
	@RequestMapping("/modifyAliase")
	@ResponseBody
	private Object modifyPersonalInfo(
			@RequestParam("id") String id,
			@RequestParam("aliase") String aliase, 
			HttpServletRequest request, HttpServletResponse resp)
	{
		BaseResp br = new BaseResp();
		User u = userService.getUserById(Long.parseLong(id));
		u.setSuAliases(aliase);
		userService.update(u);
		return buildSuccessResp(br);
	}
	
	@RequestMapping("/registByPhone")
	@ResponseBody
	private Object registByPhone(
			@RequestParam("suTel") String phone,
			@RequestParam("suPassword") String password,
			@RequestParam("yzm") String code,
			HttpServletRequest request, HttpServletResponse resp)
	{
		BaseResp br = new BaseResp();
		if(TextUtils.isEmpty(phone) || TextUtils.isEmpty(password) || TextUtils.isEmpty(code))
		{
			br = buildParamIncompleteResp(new BaseResp());
			return br;
		}else
		{
			User u = userService.findUserByTel(phone);
			if(u != null)
			{
				buildInnerErrorResp(br);
				br.setInfo("用户已存在");
				return br;
			}else
			{
				User u2 = new User();
				u2.setSuPhoto("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488867765104&di=b544e99c679526a9f15650c48d0d4995&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01e50655fa144f6ac7251df8789504.png");
				u2.setSuTel(phone);
				u2.setSuSex("male");
				u2.setSuAccount(phone);
				u2.setSuPassword(password);
				u2.setToken(generateToken(phone));
				u2.setSuUserType("1");
				u2.setSuAccountStatus("1");
				u2.setSuRegisterTime(new Date());
				userService.addUser(u2);
				MapInfo mi = new MapInfo();
				mi.user = u2;
				br.setMapInfo(mi);
				return buildSuccessResp(br);
			}
		}
	}
}
